#pragma once



#include "ILibavResample.h"
#include <atlbase.h>
#include <atlpath.h>

extern "C"
{
#include "avutil.h"
#include "avcodec.h"
#include "avformat.h"
#include "fifo.h"
}


class COptionParameter;
class CMediaFrame;
class CAudioFrame;
class CVideoFrame;
struct AVFormatParameters;
class CMediaProp;

#define MAX_AUDIO_OUT_SIZE (4*128*1024)
#define MAX_VIDEO_OUT_SIZE (8*256*1024)


class CFFCore
{
public:
	CFFCore();
	~CFFCore(void);


public:		
	bool SetSink(char* file);
	bool SetOption(COptionParameter* pOption);
	bool SetSrcProp(CMediaProp* pProp);
	void CreateVideoStream(BOOL bVideo);
	void CreateAudioStream(BOOL bAudio);
	bool EncodeFrame(CMediaFrame* p);
	bool InitFfmpeg();
	bool WriteEnd();

	
private:
	void checkExt();	
	bool write_audio_frame(CAudioFrame* pFrame);
	bool write_video_frame(CVideoFrame* pFrame);
	bool write_video_end();
	bool write_audio_end();
	bool write_streams_end();
	AVStream *add_audio_stream(int codec_id);
	AVStream *add_video_stream(int codec_id);
	bool open_video();
	bool open_audio();
	void closeFormatContext();

private:
	COptionParameter*   m_pSetting;
	CMediaProp* m_pProp;
	CPathA* m_pMediaFile;

	BOOL m_fCreateVideoStream;
	BOOL m_fCreateAudioStream;	

	AVOutputFormat *	m_pFileOutFormat;
	AVFormatContext*	m_pFormatContext;
	AVFormatParameters* m_pFormatParameter;
	AVStream* m_pAudioSt ;
	AVStream* m_pVideoSt ; 
	uint8_t* m_pAudioOutBuf;
	uint8_t* m_pVidoeOutBuf;
	uint8_t* m_pAudioFifoBuf;
	AVFifoBuffer* fifo;
	int64_t m_nTotalFrameNumber;
	int64_t m_nFTP;
	bool m_bInit;
	int m_nVideoOutSize;
	ILibavImgResample* m_pImgRmp;
	ILibavAuResample*  m_pAudRmp;

};
